package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.dto.CoursePubDTO;
import com.xuecheng.api.content.model.qo.QueryCourseBaseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.content.common.util.UAASecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.convert.CoursePubConvert;
import com.xuecheng.content.entity.CoursePub;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private CoursePubService coursePubService;

    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params, @RequestBody QueryCourseBaseModel model) {

        Long companyId = UAASecurityUtil.getCompanyId();

        PageVO pageVO = courseBaseService.queryCourseBaseList(params, model, companyId);
        return pageVO;
    }

    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO vo) {

        // 1.获得公司的id
        Long companyId = UAASecurityUtil.getCompanyId();

        // 2.将vo转为dto
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(vo);
        dto.setCompanyId(companyId);


        CourseBaseDTO result = courseBaseService.createCourseBase(dto);

        return result;
    }

    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO vo) {

        // 1.获得公司的id
        Long companyId = UAASecurityUtil.getCompanyId();

        // 2.将vo转为dto
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(vo);
        dto.setCompanyId(companyId);

        CourseBaseDTO result = courseBaseService.modifyCourseBase(dto);

        return result;
    }

    @GetMapping("course/{courseId}")
    public CourseBaseDTO getCourseBaseById(@PathVariable Long courseId) {

        Long companyId = UAASecurityUtil.getCompanyId();

        CourseBaseDTO dto = courseBaseService.getCourseBaseById(courseId, companyId);

        return dto;
    }


    @DeleteMapping("course/{courseId}")
    public CourseBaseDTO removeCourseBaseById(@PathVariable Long courseId) {

        Long companyId = UAASecurityUtil.getCompanyId();

        CourseBaseDTO dto = courseBaseService.getCourseBaseById(courseId, companyId);

        return dto;

    }


    @GetMapping("course/commit/{courseId}")
    public void commitCourseBase(@PathVariable Long courseId) {
        Long companyId = UAASecurityUtil.getCompanyId();

        courseBaseService.commitCourseBase(courseId, companyId);
    }

    @Override
    public void removeCoursebase(Long courseBaseId) {
        Long companyId = UAASecurityUtil.getCompanyId();

        courseBaseService.removeCourseBase(courseBaseId, companyId);
    }

    /*
     * Spring-Web 对 Controller 实现多视图解析器并存
     *     当前的Controller默认使用的解析器：Jackson解析器（默认）
     *       原因：controller使用了@RestController
     *     preview方法要求使用Freemarker视图解析器
     *       如果Freemarker视图解析器不是默认的，
     *           需要通过下面操作才可以实现一个Controller多个解析器并存
     *               1.调用Freemarker启动器（本工程已经导入了）
     *               2，在配置信息中开启Freemarker解析器支持
     *                   spring.freemarker.enabled = true
     *               3.在controller的方法上的返回值必须是ModelAndView对象
     * */
    @GetMapping("course/preview/{courseId}/{companyId}")
    public Object preview(@PathVariable Long courseId, @PathVariable Long companyId) {

        // 构建页面模板和数据模型生成html页面

        // service层可以获得数据模型DataMap
        Map<String, Object> dataMap = coursePubService.preview(courseId, companyId);


        // controller将数据模型和页面模板结合起来生成html
        ModelAndView modelAndView = new ModelAndView("learing_article", dataMap);


        return modelAndView;
    }

    @PostMapping("course_pub/publish/{courseBaseId}")
    public void publish(@PathVariable Long courseBaseId) {
        Long companyId = UAASecurityUtil.getCompanyId();

        coursePubService.publish(courseBaseId, companyId, false);
    }


    @GetMapping("course/l/{courseBaseId}")
    public CoursePubDTO getCourseBaseByIdNoCompanyId(@PathVariable Long courseBaseId) {

        CoursePubDTO dto = coursePubService.getCourseBaseByIdNoCompanyId(courseBaseId);

        return dto;
    }

    @GetMapping("l/{targetId}")
    public RestResponse<CourseBaseDTO> getCourseBaseByTargetId(@PathVariable Long targetId) {
        return coursePubService.getCourseBaseByTargetId(targetId);

    }

    @GetMapping("/l/coursePub/{courseName}")
    RestResponse<CoursePubDTO> getByCourseName4s(@PathVariable String courseName) {
        return coursePubService.getByCourseName4s(courseName);
    }

    //远程调用-课程内容管理服务-根据课程id查找coursePub信息
    @GetMapping("/l/coursePub/name/{coursePubId}")
    public RestResponse<CoursePubDTO> getByCourse4s(@PathVariable Long coursePubId) {
        CoursePub coursePub = coursePubService.getById(coursePubId);
        CoursePubDTO coursePubDTO = CoursePubConvert.INSTANCE.entity2dto(coursePub);
        return RestResponse.success(coursePubDTO);
    }

    @GetMapping("/l/coursePubName/{workId}")
    RestResponse<List<String>> getCoursePubByWorkId4s(@PathVariable Long workId){
        return coursePubService.getCoursePubByWorkId4s(workId);
    }

}
